package com.seventeen.monster.mq.proxy.server.proxy;

import com.seventeen.monster.common.utils.StringUtils;
import com.seventeen.monster.mq.proxy.server.proxy.consumer.ConsumeStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 消息代理接口调用
 *
 * @author seventeen
 */
public class MqClient
{

    public static final int SUCCESS_RETURN_CODE = 200;
    private static final Logger LOGGER = LoggerFactory.getLogger(MqClient.class);
    private static final int HTTP_CONNECT_TIMEOUT = 5000;
    private static final int HTTP_CONNECTION_REQUEST_TIMEOUT = 1000;
    private static final int HTTP_SOCKET_TIMEOUT = 60000;

    public static ConsumeStatus consumer(String url, String convertJson)
    {
        CloseableHttpClient httpClient = HttpClients.custom().build();
        String postJson = null;
        try
        {
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(HTTP_CONNECT_TIMEOUT)
                    .setConnectionRequestTimeout(HTTP_CONNECTION_REQUEST_TIMEOUT)
                    .setSocketTimeout(HTTP_SOCKET_TIMEOUT).build();
            HttpPost post = new HttpPost(url);
            post.setConfig(requestConfig);
            post.setHeader("Accept", "application/json");
            post.setHeader("Content-type", "application/json; charset=UTF-8");

            StringEntity entity;
            try
            {
                entity = new StringEntity(convertJson, StandardCharsets.UTF_8);
                entity.setContentType("application/json");
                post.setEntity(entity);
                CloseableHttpResponse httpResponse = httpClient.execute(post);
                StatusLine statusLine = httpResponse.getStatusLine();
                if (statusLine != null && statusLine.getStatusCode() != SUCCESS_RETURN_CODE)
                {
                    LOGGER.error("send proxyMessage {} to {} failure. Response code {} ", convertJson, url, statusLine.getStatusCode());
                }
                postJson = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                if (StringUtils.isBlank(postJson))
                {
                    LOGGER.error("send proxyMessage {} to {} failure. postJson is null", convertJson, url);
                    return ConsumeStatus.FAIL;
                }
                if (ConsumeStatus.SUCCESS.toString().equalsIgnoreCase(postJson))
                {
                    LOGGER.info("send proxyMessage {} to {} success. postJson is {}", convertJson, url, postJson);
                    return ConsumeStatus.SUCCESS;
                }
            } catch (Exception e)
            {
                LOGGER.error("send proxyMessage {} to {} failure.", convertJson, url, e);
            }
        } finally
        {
            try
            {
                httpClient.close();
            } catch (IOException e)
            {
                LOGGER.error(e.getMessage(), e);
            }
        }

        LOGGER.error("send proxyMessage {} to {} failure. postJson is {}", convertJson, url, postJson);
        return ConsumeStatus.FAIL;
    }


}
